package com.songcha.module_mine.ui.activity.bind_phone

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.songcha.library_base.mvvm.base.BaseMvvmActivity
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.RegexUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.module_mine.R
import com.songcha.module_mine.databinding.MineActivityBindPhoneBinding
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit


class BindPhoneActivity :BaseMvvmActivity<BindPhoneViewModel,MineActivityBindPhoneBinding>() {
    private var mSendVerifyDispose:Disposable?=null


    override fun getLayoutId(): Int {
        return R.layout.mine_activity_bind_phone
    }

    override fun isImmerseStatusBar(): Boolean {
        return true
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        //databinding.loginEdtPhone.setText("13694278416")
        intent?.let {
            viewModel.isChangeBindPhone=it.getBooleanExtra("is_change",false)
        }

        if(viewModel.isChangeBindPhone){
            databinding.mineCclOriginPhone.visibility=View.VISIBLE
            databinding.mineTvTitle.text="修改绑定手机"
            databinding.mineBtnBind.text="换绑"
        }

        viewModel.verifyCountDown.observe(this){
            updateTvSendVerify(it)
        }

        viewModel.sendVerifySuccess.observe(this){
            showSendVerifyResult(it)
        }

        viewModel.bindPhoneState.observe(this){data->
            if(data){
                ToastUtil.show("换绑成功")
                finish()
            }
        }


        databinding.mineIvBack.setOnClickListener {
            finish()
        }

        databinding.mineTvSendVerify.setOnClickListener {
            sendVerify()
        }

        databinding.mineBtnBind.setOnClickListener{
            if(viewModel.isChangeBindPhone){
                changeBindPhone()
            }else{
                bindPhone()
            }

        }

    }



    private fun showSendVerifyResult(isSuccess: Boolean){
        if(isSuccess){
            ToastUtil.show("验证码发送成功")
        }else{
            mSendVerifyDispose?.dispose()
        }
    }

    private fun updateTvSendVerify(countDown:Int){
        if(countDown==0){
            databinding.mineTvSendVerify.text="发送验证码"
            //databinding.mineTvSendVerify.setTextColor(Color.parseColor("#635F5F"))
        }else{
            databinding.mineTvSendVerify.text="重新获取（${countDown}s）"
            //databinding.mineTvSendVerify.setTextColor(Color.parseColor("#6D90E0"))
        }

    }

    private fun bindPhone(){
        val phone=databinding.mineEdtPhone.text.toString().trim()
        if(!checkPhone(phone)) return
        val verifyCode=databinding.mineEdtVerify.text.toString().trim()
        if(verifyCode.isEmpty()){
            ToastUtil.show(resources.getString(R.string.mine_please_input_verify))
            return
        }
        viewModel.changeBindPhone(phone,verifyCode)

    }

    private fun changeBindPhone(){
        val originPhone=databinding.mineEdtOriginPhone.text.toString().trim()
        val phone=databinding.mineEdtPhone.text.toString().trim()
        if(!checkPhone(phone)) return
        if(!checkOriginPhone(originPhone,phone)) return

        val verifyCode=databinding.mineEdtVerify.text.toString().trim()
        if(verifyCode.isEmpty()){
            ToastUtil.show(resources.getString(R.string.mine_please_input_verify))
            return
        }
        viewModel.changeBindPhone(phone,verifyCode)

    }


    private fun sendVerify(){
        if(viewModel.verifyCountDown.value!!>0){
            return
        }

        val phone=databinding.mineEdtPhone.text.toString().trim()
        if(!checkPhone(phone)) return

        if(viewModel.isChangeBindPhone){
            val originPhone=databinding.mineEdtOriginPhone.text.toString().trim()
            if(!checkOriginPhone(originPhone,phone)) return
        }

        mSendVerifyDispose?.dispose()
        mSendVerifyDispose = Observable.intervalRange(0,60,0,1,TimeUnit.SECONDS)
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe{
                viewModel.setCountDown(59-it.toInt())
            }

        viewModel.sendVerify(phone)
    }

    private fun checkPhone(phone:String):Boolean{
        if(phone.isEmpty()){
            ToastUtil.show(resources.getString(R.string.mine_please_input_phone))
            return false
        }
        if(!RegexUtil.isPhone(phone)){
            ToastUtil.show(resources.getString(R.string.mine_invalid_phone))
            return false
        }
        return true
    }

    private fun checkOriginPhone(originPhone:String,phone:String):Boolean{
        if(originPhone.isEmpty()){
            ToastUtil.show(resources.getString(R.string.mine_please_input_origin_phone))
            return false
        }
        if(!RegexUtil.isPhone(originPhone)){
            ToastUtil.show(resources.getString(R.string.mine_invalid_phone))
            return false
        }
        if(UserInfoManager.userInfo!=null && originPhone!=UserInfoManager.userInfo!!.phonenumber){
            ToastUtil.show("原手机号码不正确")
            return false
        }
        if(viewModel.isChangeBindPhone && phone==originPhone){
            ToastUtil.show("新手机号不能与原手机号一致")
            return false
        }

        return true
    }


}